<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>First Ruby Program: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="best rubies guide,best ruby guide,learn ruby websites,ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,rbuy,ruyb,rubie,rubi,urby,ruby on rails,ruby rails,raisl,ruby on rails consulting,ruby learning,quick ruby guide,ruby services,ruby tutoring,student loans,distant learning,tutoring,online learning,remote desktop,online training,computer learning,learning,learning services,learning ruby,institute for learning,ruby programming,ruby on rails servers,ruby on rails hosting,ruby on rails development,developing with ruby,ruby training" />
<meta name="keywords" content="ruby program,ruby study notes,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />

<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="/images/favicon.ico" />
<link href="https://plus.google.com/104011292189143717925/" rel="publisher" />
<!-- Google +1 button code -->
<link rel="canonical" href="/satishtalim/first_ruby_program.html" />
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Helping Ruby Programmers become Awesome!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="https://blog.rubylearning.github.io/" title="Ruby blog of Ruby Learning site">Blog</a></li>
            <li><a href="http://courses.rubylearning.org/" title="Online Ruby Course">Online Course</a></li>
            <li><a href="http://ruby-challenge.rubylearning.github.io/" title="Ruby Programming Challenge for Newbies">Challenge</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">


            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>My First Ruby Program</h1>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_installation.html">&lt;Installation | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/features.html">Features&gt;</a>
                  </strong>
                </p>

                <p>Let's open up our plain-text editor. As far as possible, ensure that your editor's Tab is set to 2 spaces. <i>We are now ready to write our first Ruby program</i>.</p>

                <p class="post-note">Code layout is pretty much up to you; indentation is not significant (but using two-character indentation will make you friends in the community if you plan on distributing your code).</p>

                <p>Create a folder named, say. <i>rubyprograms</i>. We shall store all our programs in this folder. Our first program will display the string 'Hello' on the command window and the name of the program will be say <strong>p001hello.rb</strong> </p>

                <p>By convention, Ruby source files have the <strong>.rb</strong> file extension. In Microsoft Windows, Ruby source files sometimes end with .rbw, as in myscript.rbw. The Ruby coding convention states that file/directory name is lower case of class/module name with .rb extension. For example, Foo class has name foo.rb</p>

                <p>Type the following in your editor:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # p001hello.rb
                puts 'Hello'
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>and then click File/Save As... Give the name <strong>p001hello.rb</strong> and store it in your <i>rubyprograms</i> folder. To run your program, open a command window and type <b>ruby p001hello.rb</b> as shown below:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                c:\rubyprograms> ruby p001hello.rb
                Hello
                c:\rubyprograms>
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>You should see the output as shown above.</p>

                <p><strong>Note</strong>: Ruby is a scripting language. There is no special main method in Ruby from which execution begins. The Ruby interpreter is given a script of statements to execute, and it begins executing at the first line and continues to the last line. <strong>puts</strong> (s in puts stands for string; <strong>puts</strong> really means put string) simply writes onto the screen whatever comes after it, but then it also automatically goes to the next line.</p>

                <p class="post-note">a. Parentheses are usually optional with a method call. These calls are all valid:<br />foobar<br />foobar()<br />foobar(a, b, c)<br />foobar a, b, c<br /><br />b. In Ruby, everything from an integer to a string is considered to be an object (more on this later). And each object has built in 'methods' (Ruby term for functions) which can be used to do various useful things. To use a method, you need to put a dot after the object, and then append the method name. Some methods such as <strong>puts</strong> and <strong>gets</strong> are available everywhere and don't need to be associated with a specific object.<br />Technically speaking, these methods are provided by Ruby's <strong>Kernel</strong> module (more on this later) and they are included in all Ruby objects (the <strong>Kernel</strong> module is included by class (more on this later) <strong>Object</strong>, so its methods are available in every Ruby object). When you run a Ruby application, an object called <strong>main</strong> of class <strong>Object</strong> is automatically created. This object provides access to the <strong>Kernel</strong> methods.</p>

                <h3>Observe:</h3>

                <ol>
                <li>Java and C programmers - no need to write a main method/function</li>
                <li>String literals are sequences of characters between single or double quotation marks.</li>
                <li>Ruby is an interpreted language, so you don't have to recompile to execute the program written in Ruby</li>
                <li>The Ruby coding convention states that file/directory name is lower case of class/module name with .rb extension. For example, Foo class has name foo.rb</li>
                </ol>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them. Much of the material on <a href="/">rubylearning.github.io</a> and in the course at <a href="http://rubylearning.org/">rubylearning.org</a> is drawn <strong>primarily</strong> from the <strong>Programming Ruby book</strong>, available from <a href="https://pragprog.com/book/ruby4/programming-ruby-1-9-2-0">The Pragmatic Bookshelf</a>.</p>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_installation.html">&lt;Installation | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/features.html">Features&gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">
            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
</div>
<!-- footer ends here -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
